package com.ptmall.app.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpFragment;
import com.ptmall.app.bean.LogBean;
import com.ptmall.app.bean.MessageBean;
import com.ptmall.app.bean.PageData;
import com.ptmall.app.net.HttpMethods;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.api.ApiService;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.ui.activity.WebActivity;
import com.ptmall.app.ui.adapter.MsgListAdapter;
import com.ptmall.app.ui.adapter.OrderNoticeAdapter;
import com.ptmall.app.view.PullToRefreshLayout;
import com.ptmall.app.view.PullableListView;

import java.util.ArrayList;
import java.util.List;

public class LogFragment extends BaseMvpFragment {

    PullableListView mListview;
    PullToRefreshLayout mRefreshView;

    ApiDataRepository apiDataRepository = ApiDataRepository.getInstance();
    OrderNoticeAdapter mOrderNoticeAdapter;
    View view;
    int page = 1;
    List<LogBean> mDataLists = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_list,container,false);
        return view;

    }

    @Override
    public void initViews() {

        mRefreshView = (PullToRefreshLayout) view.findViewById(R.id.refresh_view);
        mListview = (PullableListView) view.findViewById(R.id.listview);
        mRefreshView.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;

                getlist();
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page ++;
                getlist();

            }
        });
    }

    @Override
    public void initEvents() {

        mOrderNoticeAdapter = new OrderNoticeAdapter(getContext(),null);
        mListview.setAdapter(mOrderNoticeAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        getlist();
    }

    private void getlist(){
        apiDataRepository.getOrderNotice(null, new ApiNetResponse<PageData<LogBean>>() {
            @Override
            public void onSuccess(PageData<LogBean> data) {
                if (null != mRefreshView) {
                    mRefreshView.refreshFinish(PullToRefreshLayout.SUCCEED);
                    mRefreshView.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if (page == 1) {
                    mDataLists.clear();
                    if (null != data.getData() && data.getData().size() > 0) {
                        mDataLists.addAll(data.getData());
                    } else {
                    }
                } else {
                    if (null != data.getData() && data.getData().size() > 0) {
                        mDataLists.addAll(data.getData());
                    } else {
                        showMsg(R.string.has_no_more);
                    }
                }
                mOrderNoticeAdapter.pushData(data.getData());
            }
        });
    }
}
